package com.ofps.forms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ofps.forms.common.utils.R;
import com.ofps.forms.mapper.QuestionDatetimeMapper;
import com.ofps.forms.domain.QuestionDatetime;
import com.ofps.forms.service.QuestionDatetimeService;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.ofps.common.utils.SecurityUtils.getUserId;


@Service("questionDatetimeService")
public class QuestionDatetimeServiceImpl extends ServiceImpl<QuestionDatetimeMapper, QuestionDatetime> implements QuestionDatetimeService {
    @Resource
    private QuestionDatetimeMapper questionDatetimeMapper;

    @Override
    public R delete(Map<String, Object> body) {
        QueryWrapper<QuestionDatetime> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", body.get("id"));
        return 1 == questionDatetimeMapper.delete(queryWrapper) ? R.ok("删除成功！") : R.error("删除失败！");
    }

    @Override
    public R save(HttpServletRequest request, QuestionDatetime questionDatetime) {
//        questionDatetime.setUserId(TokenUtil.getId(request));
        questionDatetime.setUserId(Math.toIntExact(
                getUserId()));
        return this.save(questionDatetime) ? R.ok("创建填空题成功！") : R.error("创建失败！");
    }

    @Override
    public List<QuestionDatetime> queryQuestionDatetimeEntity(Map<String, Object> body) {
        QueryWrapper<QuestionDatetime> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", body.get("question_id"));
        return questionDatetimeMapper.selectList(queryWrapper);
    }
}
